const app = getApp()
import request from './request.js'
import common from './common.js'

class login {
	static login_judge(data){ //登录后的判断
		const self = this;
		// 新增机构选择:登录成功后去机构选择
		if(data.length == 1){
			// 如果只有一个机构
			if(app.globalData.isWxwork){
				// 如果为企业微信,则注册
				let params = {
					open_userid: app.globalData.open_userid,
					token: data[0].token
				}
				uni.request({
					url:app.globalData.baseUrl + '/qywechat/bindQyuser',
					data:params,
					method:"POST",
					header:{
						"Content-Type":"application/x-www-form-urlencoded"
					},
					success(e) {
						// 绑定成功或重复绑定都跳转
						if(e.data.code == 200){
							self.saveUserOpenId(app.globalData.open_userid)
						}else if(e.data.code == 429){
							self.saveUserOpenId(data[0].open_userid)
						}
						if(e.data.code == 200 || e.data.code == 429){
							uni.showToast({
								icon:"success",
								title:"登录成功",
								mask:true,
								duration:600
							})
							self.saveUserInfo(data[0])
							setTimeout(()=>{
								common.go('/pages/index/index',2)
							},600)
						}
					}
				})
			}else{
				uni.showToast({
					icon:"success",
					title:"登录成功",
					mask:true,
					duration:600
				})
				self.saveUserInfo(data[0])
				setTimeout(()=>{
					common.go('/pages/index/index',2)
				},600)
			}
		}else if(data.length > 1){
			uni.showToast({
				icon:"success",
				title:"登录成功",
				mask:true,
				duration:600
			})
			setTimeout(()=>{
				common.go('/pages/index/chooseJiGou?showBack=0')
			},600)
		}
		this.saveUserJiGou(data,data[0].id)
	}
	
	static saveUserInfo(data){
		app.globalData.userInfo = data
		// console.log("token:",data.token)
		// 保存到本地缓存
		uni.setStorage({
			key:"userInfo",
			data:JSON.stringify(data)
		})
	}
	
	static saveUserJiGou(data,id){
		app.globalData.userJiGou = data
		app.globalData.userJiGou_id = id
		uni.setStorage({
			key:"userJiGou",
			data:JSON.stringify(data)
		})
		uni.setStorage({
			key:"userJiGou_id",
			data:id
		})
	}
	
	static saveUserOpenId(open_userid){
		app.globalData.open_userid = open_userid
		uni.setStorage({
			key:"open_userid",
			data:open_userid
		})
	}
	
	static checkLogin(){
		return new Promise((resolve, reject) => {
			if(app.globalData.userInfo){
				resolve(true)
			}else{
				uni.getStorage({
					key:"userInfo",
					success(e){
						// 获取用户机构
						uni.getStorage({
							key:"userJiGou",
							success(res) {
								if(res.data != undefined){
									let userJiGou = JSON.parse(res.data)
									app.globalData.userJiGou = userJiGou
								}
							}
						})
						// 获取用户机构选中的id
						uni.getStorage({
							key:"userJiGou_id",
							success(res) {
								if(res.data != undefined){
									let userJiGou_id = res.data
									app.globalData.userJiGou_id = userJiGou_id
								}
							}
						})
						// 企业微信时获取open_userid
						if(app.globalData.isWxwork){
							uni.getStorage({
								key:"open_userid",
								success(res) {
									if(res.data != undefined){
										let open_userid = res.data
										app.globalData.open_userid = open_userid
									}
								}
							})
						}
						if(e.data != undefined){
							let userInfo = JSON.parse(e.data)
							// console.log("token:",userInfo.token)
							app.globalData.userInfo = userInfo
							resolve(true)
						}else{
							resolve(false)
						}
					},
					fail() {
						resolve(false)
					}
				})
			}
		})
	}
}

export default login